import pymysql

conn = pymysql.connect(host="localhost",port=3306,user="root",
password="zxczxc!!",database="test_db",charset="utf8")

cursor = conn.cursor()
try:
    sql_i = "insert into t_book(title, pub_date) values ('西游记','1986-01-01')"
    sql_u = "update t_book set `read` = `read` + 1 where title = '西游记'"
    sql_d = "delete from t_book where title = '西游记'"
    cursor.execute(sql_d)
    conn.commit() #提交事务
    print(conn.affected_rows())
except Exception as e:
    conn.rollback()
    print(e)
finally:
    cursor.close()
    conn.close()

